﻿using System;
using OSCSchema;
using OutlookSocialProvider;

namespace OSC.Base
{
    public abstract class OSCProviderBase : ISocialProvider
    {
        #region Private fields
        private readonly OSCSerializer m_Serializer = new OSCSerializer();
        private ISocialSession m_SocialSession;
        #endregion

        #region Properties
        public virtual int SynchRestartInterval { get { return 0; } }
        protected virtual string CreateAccountUrl { get { return string.Empty; } }
        protected virtual bool DisplayUrl { get { return false; } }
        protected virtual bool UnfollowPerson { get { return true; } }
        protected virtual bool FollowPerson { get { return true; } }
        protected virtual bool RememberMyPassword { get { return true; } }
        protected virtual string ForgotPasswordUrl { get { return string.Empty; } }
        protected virtual AuthenticationTypes AuthenticationType { get { return AuthenticationTypes.Forms; } }

        public abstract string Version { get; }
        public abstract string[] DefaultSiteUrls { get; }
        public abstract byte[] SocialNetworkIcon { get; }
        public abstract string SocialNetworkName { get; }
        public abstract Guid SocialNetworkGuid { get; }
        #endregion

        #region Methods
        public string GetCapabilities()
        {
            var capabilities = new capabilities
                                   {
                                       getActivities = true,
                                       cacheActivities = true,
                                       cacheFriends = true,
                                       contactSyncRestartInterval = (uint)SynchRestartInterval,
                                       contactSyncRestartIntervalSpecified = SynchRestartInterval > 0,
                                       createAccountUrl = CreateAccountUrl,
                                       displayUrl = DisplayUrl,
                                       doNotFollowPerson = UnfollowPerson,
                                       dynamicActivitiesLookup = true,
                                       dynamicActivitiesLookupEx = true,
                                       dynamicActivitiesLookupExSpecified = true,
                                       dynamicActivitiesLookupSpecified = true,
                                       dynamicContactsLookup = true,
                                       dynamicContactsLookupSpecified = true,
                                       followPerson = FollowPerson,
                                       forgotPasswordUrl = ForgotPasswordUrl,
                                       getFriends = true,
                                       hashFunction = "MD5",
                                       hideHyperlinks = string.IsNullOrEmpty(CreateAccountUrl) && string.IsNullOrEmpty(ForgotPasswordUrl),
                                       hideHyperlinksSpecified = true,
                                       hideRememberMyPassword = !RememberMyPassword,
                                       hideRememberMyPasswordSpecified = true,
                                       showOnDemandActivitiesWhenMinimized = true,
                                       showOnDemandActivitiesWhenMinimizedSpecified = true,
                                       showOnDemandContactsWhenMinimized = true,
                                       showOnDemandContactsWhenMinimizedSpecified = true,
                                       supportsAutoConfigure = false,
                                       supportsAutoConfigureSpecified = true,
                                       useLogonCached = true,
                                       useLogonCachedSpecified = true,
                                       useLogonWebAuth = AuthenticationType == AuthenticationTypes.Forms
                                   };

            return m_Serializer.Serialize(capabilities);
        }
        public ISocialSession GetSession()
        {
            return m_SocialSession;
        }
        public ISocialSession GetAutoConfiguredSession()
        {
            throw new NotSupportedException();
        }
        public void GetStatusSettings(out string statusDefault, out int maxStatusLength)
        {
            //This method is currently not supported by OSC
            throw new NotSupportedException();
        }
        public void Load(string socialProviderInterfaceVersion, string languageTag)
        {
            var session = CreateSession();
            m_SocialSession = new CachedSession(session, new IsolatedStorageCache(SocialNetworkGuid, session, SynchRestartInterval));
        }
        public abstract ISocialConnector CreateSession();
        #endregion
    }
}